package com.dd.controoler;

import com.dd.mapper.AddressMapper;
import com.dd.mapper.OrderMaper;
import com.dd.mapper.UserOrderMapper;
import com.dd.pojo.Address;
import com.dd.pojo.Order;
import com.dd.pojo.User;
import com.dd.pojo.Userorder;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@Controller
public class OdersController {
    @Autowired
    private OrderMaper orderMaper;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private UserOrderMapper userOrderMapper;

    @PostMapping("addOrder")
    public String addOrder(String id, Integer counts, HttpServletRequest request) {
        try {
            Integer id1 = Integer.parseInt(id);
            Order order = orderMaper.findByAddressId(id1);
            Address address = addressMapper.findById(id1);
            String uuid = "";
            User user1 = (User) request.getSession().getAttribute("user");
            if (order != null) {
                Integer number = address.getNumbers();
                 if(order.getNumber()>=counts){
                     double s = address.getCosts() / (order.getCosts() + counts);
                     order.setPrice(s);
                     uuid = order.getUuid();
                     order.setCosts(order.getCosts() + counts);
                     order.setNumber(number-order.getCosts());
                     orderMaper.upte(order);
                 }
            } else {
                Integer number = address.getNumbers();
                if(number>=counts){
                    double costs = address.getCosts();
                    double s = costs / counts;
                    Order order1 = new Order();
                    order1.setUuid(UUID.randomUUID().toString());
                    order1.setPrice(s);
                    order1.setAddress_id(address.getId());
                   Integer numer=address.getNumbers()-counts;
                    order1.setNumber(numer);
                    order1.setCosts(counts);
                    orderMaper.save(order1);
                    uuid = order1.getUuid();
                }
            }
            List<Userorder> userId = userOrderMapper.findByUserId(user1.getId());
            if (userId.size() <=0) {

                Userorder userorder = new Userorder();
                User user = (User) request.getSession().getAttribute("user");
                userorder.setUser_id(user.getId());
                userorder.setOrder_id(uuid);
                userOrderMapper.save(userorder);


            }
            List<Userorder> byUserId = userOrderMapper.findByUserIdUUID(user1.getId(), uuid);
            if (byUserId.size()<=0) {
                Userorder userorder = new Userorder();
                User user = (User) request.getSession().getAttribute("user");
                userorder.setUser_id(user.getId());
                userorder.setOrder_id(uuid);
                userOrderMapper.save(userorder);
            }
        } catch (NumberFormatException e) {
            request.setAttribute("msg","打车失败");
            return "redirect:loginDate";
        }
        return "redirect:loginDate";
    }
}
